
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade.helpers;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import oxygen.tool.wlfacade.WLFacadeContextHelper;
import oxygen.util.CloseUtils;
import oxygen.util.ProcessHandler;

public class WLServerStart {
  
  public static void startserverprocess(WLFacadeContextHelper wlctx, String outerrfile) 
    throws Exception {
    startserverprocess(wlctx, null, null, null, outerrfile);
  }
  
  public static void startserverprocess(WLFacadeContextHelper wlctx, Map sysprops, 
                                        String startdir, String[] env, String outerrfile) 
    throws Exception {
    // get runtime mbean and call startserver on it
    ArrayList al = new ArrayList();
    al.add(System.getProperty("java.home") + "/bin/java");
    if(sysprops != null) {
      for(Iterator itr = sysprops.keySet().iterator(); itr.hasNext(); ) {
        Object key = (Object)itr.next();
        al.add("-D" + key + "=" + sysprops.get(key));
      }
    }
    al.add("weblogic.Server");
    String[] cmdarr = (String[])al.toArray(new String[0]);
    Writer outerrfw = new FileWriter(outerrfile);
    if(startdir == null) {
      startdir = ".";
    }
    Process p = Runtime.getRuntime().exec(cmdarr, env, new File(startdir));
    ProcessHandler.handle(p, outerrfw, null, false);
    CloseUtils.close(outerrfw);
  }
  
}

